<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {
	
	 public function __construct()
    {
        parent::__construct();
        session_start();
        //$this->output->enable_profiler(FALSE);
        
        $this->load->model('MStatic');
        $this->load->model('MUser');
    }
	    
	public function index()
	{
	    if(isset($_SESSION['username'])){
	       redirect('/','refresh');
        }
        if ($this->input->post('txtus')){
			$u = $this->input->post('txtus');
			$pw = $this->input->post('txtps');
			$branch = $this->input->post('cobranchlogin');
            $this->MUser->verifyUser($u,$pw,$branch);
            
            //$_SESSION['username'] = $u;
            //$_SESSION['branch'] = $branch;
            redirect('/','refresh');
		}
        $data['title'] = "Administrator Login";
        $data['branchList'] = $this->MStatic->getBranchDropdown();
		$this->load->vars($data);
		$this->load->view('login'); 
	}
	
	public function setting() {
		if (!isset($_SESSION['uid']) || $_SESSION['uid'] < 1){
		   	redirect('memberlogin/requiredmessage','refresh');
		}
		
		if ($this->input->post('submit')){
	  		if($_SESSION['role_id']==1) {	
                $this->MStaff->updateStaff();
                $this->session->set_flashdata('message','Account updated');
	  			redirect('hradmin','refresh');
			}
	  		elseif($_SESSION['role_id']==2) {	
	  		    $this->MStaff->updateAccountSetting();
                $this->session->set_flashdata('message','Account updated');
				redirect('employee','refresh');
			}
	  		
	  	}else{
			$data['title'] = "Account Setting";
			$data['main'] = 'account_form_setting';
			$data['roles'] = $this->MUser->getRoles();
			$data['status'] = $this->MStaff->getStatusDropdown();
			$data['positions'] = $this->MPosition->getPositionDropdown();	
			$data['teams'] = $this->MTeam->getTeamDropdown();
			$data['staff'] = $this->MStaff->getAccountDetailByUId($_SESSION['uid']);	
        	if($_SESSION['role_id']==1) {			
                $data['locations'] = $this->MStaff->getLocationDropDown();
                $data['lastSID'] = $this->MStaff->getLastSID(0,1);
  			    $data['form'] = $this->MFormGlobal->getFormSaveCancelBottom('/index.php/hradmin');
			}
	  		elseif($_SESSION['role_id']==2) {	
				$data['form'] = $this->MFormGlobal->getFormSaveCancelBottom('/index.php/employee');
			}
			$this->load->vars($data);
			$this->load->view('template');   
		}
	}
	
	function logout(){
		unset($_SESSION['branch']);
		unset($_SESSION['username']);
		$this->session->set_flashdata('error',"You've been logged out!");
		redirect('administration','refresh');
	 }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */